package tt.dz.biz.service.channel;

import java.util.Iterator;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import tt.dz.biz.annotation.MsLog;
import tt.dz.biz.dao.channel.ALiPayChannelCheckDaoImp;
import tt.dz.biz.dao.channel.UUBeeChannelCheckDaoImp;
import tt.dz.biz.dao.channel.WeChatChannelCheckDaoImp;
import tt.dz.common.LogUtil;
import tt.dz.vo.AlipayBillRecordVO;
import tt.dz.vo.UubeeBillRecordVO;
import tt.dz.vo.WechatBillRecordVO;
import tt.dz.common.DzConstants;
@Service("channelCheckService")
public class ChannelCheckService
{
  @Resource(name="aLiPayChannelCheckDao")
  ALiPayChannelCheckDaoImp aLiPayChannelCheckDao;
  @Resource(name="uuBeeChannelCheckDao")
  UUBeeChannelCheckDaoImp uuBeeChannelCheckDao;
  @Resource(name="weChatChannelCheckDao")
  WeChatChannelCheckDaoImp weChatChannelCheckDao;
  
  @MsLog(bizCode="ALiPaychannelCheckJob", bizName="支付宝渠道对账JOB", bizType=DzConstants.BizType.JOB, bizDesc="支付宝渠道对账JOB")
  public void executeALiPayChannelCheckJob()
    throws Exception
  {
    try
    {
      this.aLiPayChannelCheckDao.ChannelCheck();
    }
    catch (Exception e)
    {
      LogUtil.error(e.getMessage(), e);
      throw e;
    }
  }
  
  @MsLog(bizCode="uuBeechannelCheckJob", bizName="有贝渠道对账JOB", bizType=DzConstants.BizType.JOB, bizDesc="有贝渠道对账JOB")
  public void executeUUBeeChannelCheckJob()
    throws Exception
  {
    try
    {
      this.uuBeeChannelCheckDao.ChannelCheck();
    }
    catch (Exception e)
    {
      LogUtil.error(e.getMessage(), e);
      throw e;
    }
  }
  
  @MsLog(bizCode="weChatchannelCheckJob", bizName="微信渠道对账JOB", bizType=DzConstants.BizType.JOB, bizDesc="微信渠道对账JOB")
  public void executeWeChatChannelCheckJob()
    throws Exception
  {
    try
    {
      this.weChatChannelCheckDao.ChannelCheck();
    }
    catch (Exception e)
    {
      LogUtil.error(e.getMessage(), e);
      throw e;
    }
  }
  
  public int[] saveAliPayBill(List<AlipayBillRecordVO> alipayBillRecordList)
    throws Exception
  {
    for (Iterator<AlipayBillRecordVO> iterator = alipayBillRecordList.iterator(); iterator.hasNext();)
    {
      AlipayBillRecordVO alipayBillRecordVO = (AlipayBillRecordVO)iterator.next();
      String tranNo = alipayBillRecordVO.getTranNo();
      if ((tranNo == null) || ("".equals(tranNo)))
      {
        iterator.remove();
      }
      else
      {
        AlipayBillRecordVO alipaypayBillPO = this.aLiPayChannelCheckDao.getAliPayBillRecordByTranNo(tranNo);
        if (alipaypayBillPO != null) {
          iterator.remove();
        }
      }
    }
    return this.aLiPayChannelCheckDao.insertAlipayBillRecord(alipayBillRecordList);
  }
  
  public int[] saveUuBeeBill(List<UubeeBillRecordVO> uuBillRecordList)
    throws Exception
  {
    for (Iterator<UubeeBillRecordVO> iterator = uuBillRecordList.iterator(); iterator.hasNext();)
    {
      UubeeBillRecordVO uubeeBillRecordVO = (UubeeBillRecordVO)iterator.next();
      String merchantOrder = uubeeBillRecordVO.getMerchantOrder();
      if ((merchantOrder == null) || ("".equals(merchantOrder)))
      {
        iterator.remove();
      }
      else
      {
        UubeeBillRecordVO uubeeBillPO = this.uuBeeChannelCheckDao.getUubeeBillRecordByOrderNo(merchantOrder);
        if (uubeeBillPO != null) {
          iterator.remove();
        }
      }
    }
    return this.uuBeeChannelCheckDao.insertUubeeBillRecord(uuBillRecordList);
  }
  
  public int[] saveWechatBill(List<WechatBillRecordVO> wechatBillRecordList)
    throws Exception
  {
    for (Iterator<WechatBillRecordVO> iterator = wechatBillRecordList.iterator(); iterator.hasNext();)
    {
      WechatBillRecordVO wechatBillRecordVO = (WechatBillRecordVO)iterator.next();
      String pubAccountId = wechatBillRecordVO.getPubAccountId();
      String wechatOrder = wechatBillRecordVO.getWechatOrder();
      if ((pubAccountId == null) || ("".equals(pubAccountId)) || (wechatOrder == null) || ("".equals(wechatOrder)))
      {
        iterator.remove();
      }
      else
      {
        WechatBillRecordVO wechatBillPO = this.weChatChannelCheckDao.getWechatBillRecordByWechatOrder(pubAccountId, wechatOrder);
        if (wechatBillPO != null) {
          iterator.remove();
        }
      }
    }
    return this.weChatChannelCheckDao.insertWechatBillRecord(wechatBillRecordList);
  }
  
  public List<AlipayBillRecordVO> getAlipayBillRecord()
  {
    return this.aLiPayChannelCheckDao.getAlipayBillRecord();
  }
  
  public List<UubeeBillRecordVO> getUubeeBillRecord()
  {
    return this.uuBeeChannelCheckDao.getUubeeBillRecord();
  }
  
  public List<WechatBillRecordVO> getWechatBillRecord()
  {
    return this.weChatChannelCheckDao.getWechatBillRecord();
  }
}
